﻿using K8.EF;
using KingerRobot.PCRMES.Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.PCRMES.SpiService;

public class WmsOrderRepositories: IWmsOrderRepositories
{
    #region 同步方法
    public i_wms_order QueryById(string id)
    {
        var _db = Db.Default;
        return _db.WmsOrders.FirstOrDefault(d => d.LIST_NO == id);
    }

    /// <summary>
    /// 功能描述:根据ID查询数据
    /// 作　　者:Robin
    /// </summary>
    /// <param name="lstIds">id列表,如果是联合主键，请使用Where条件</param>
    /// <returns>数据实体列表</returns>
    public List<i_wms_order> QueryByIDs(object[] lstIds)
    {
        var _db = Db.Default;
        return _db.WmsOrders.Where(d => lstIds.Contains(lstIds)).ToList();
    }

    /// <summary>
    /// 更新实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public int Update(i_wms_order entity)
    {
        var _db = Db.Default;
        return _db.WmsOrders.Where(d=>d.LIST_NO== entity.LIST_NO).Update<i_wms_order>(new i_wms_order()
        {
            LIST_NO=entity.LIST_NO,
            UPDATE_FLAG = 0
        });
    }
    /// <summary>
    /// 待条件更新所有数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Update(Expression<Func<i_wms_order, bool>> whereExpression, i_wms_order entity)
    {
        var _db = Db.Default;
        return _db.WmsOrders.Where(whereExpression).Update(new i_wms_order()
        {
            UPDATE_FLAG = 0,
            LAST_MODIFIED_TIME = entity.LAST_MODIFIED_TIME,
            LAST_MODIFIED_USER = entity.LAST_MODIFIED_USER

        });
    }

    /// <summary>
    /// 正序查询第一条数据
    /// </summary>
    /// <returns></returns>
    public i_wms_order First()
    {
        var _db = Db.Default;
        return _db.WmsOrders.First();
    }
    /// <summary>
    /// 查询第一条数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public i_wms_order First(Expression<Func<i_wms_order, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.WmsOrders.Where(whereExpression).FirstOrDefault();
    }


    /// <summary>
    /// 功能描述:查询所有数据
    /// 作　　者:Robin
    /// </summary>
    /// <returns>数据列表</returns>
    public List<i_wms_order> Query()
    {
        try
        {
            var _db = Db.Default;
            return _db.WmsOrders.ToList();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Query执行异常:{ex.Message}");
        }
        return null;
    }


    /// <summary>
    /// 功能描述:查询数据列表
    /// 作　　者:Robin
    /// </summary>
    /// <param name="whereExpression">whereExpression</param>
    /// <returns>数据列表</returns>
    public List<i_wms_order> Query(Expression<Func<i_wms_order, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.WmsOrders.Where(whereExpression).ToList();
    }
    #endregion
}
